﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using FPP.Models;
using FPP.Models.Collections;
using FPP.Custom;

namespace FPP.Controllers
{
    public class RosterController : BaseController
    {
        //fppEntities context = new fppEntities(DataAccess.ffpCString);

        // The roster main view will only display the roster collection for a specific game and user
        // GET: /Roster/
        [CustomAuthorize]
        [HttpGet]
        public ActionResult Index(int id)
        {
            return View(GetUgr(id));
        }

        [CustomAuthorize]
        [HttpPost]
        public ActionResult Index(int id, FormCollection collection)
        {
            var pool = new Pool();

            var qContext = new fppEntities(DataAccess.ffpCString);
            using (qContext)
            {
                //Adding the values to the pool object.
                try
                {
                    pool.UserId = GetUserFromLoginToken(provider.SessionId).Id;
                    pool.PoolTypeId = id;
                    pool.FamousPersonId = int.Parse(collection["celebID"].ToString(CultureInfo.InvariantCulture));
                    pool.DateCreated = DateTime.Now;
                    
                    qContext.Pools.AddObject(pool);
                    qContext.SaveChanges();
                }
                catch (Exception)
                {
                    TempData["message"] =
                        "Unable to locate person, please try adding them <a href='http://famouspeoplepool.com/FamousPeople/Create'>here</a>";
                }
            }

            return View(GetUgr(id));
        }

        public ActionResult View(int id)
        {
            var userId = int.Parse(Request.QueryString["u"]);
            User player;

            using (context)
            {
                player = context.Users.Single(u => u.Id == userId);
            }

            return View(GetUgr(id, player));
        }

        private UserGameRoster GetUgr(int gameId)
        {
            User player = GetUserFromLoginToken(provider.SessionId);

            return GetUgr(gameId, player);

        }

        private UserGameRoster GetUgr(int gameId, User player)
        {
            var ugr = new UserGameRoster();

            using (context)
            {
                ugr.Player = player;
                ugr.Game = context.PoolTypes.Single(g => g.Id == gameId);
                ugr.Roster = new List<Pool>();

                var q = from p in context.Pools
                        where p.UserId == ugr.Player.Id
                        where p.PoolTypeId == gameId
                        orderby p.FamousPerson.DateOfBirth ascending
                        select p;

                foreach (Pool pool in q)
                {
                    ugr.Roster.Add(pool);
                }
            }

            return ugr;
        }

        [CustomAuthorize]
        public ActionResult RemoveFromRoster(int id, int poolId)
        {
            var qContext = new fppEntities(DataAccess.ffpCString);
            using (qContext)
            {
                try
                {
                    var pool = qContext.Pools.First(p => p.Id == poolId);
                    qContext.Pools.DeleteObject(pool);
                    qContext.SaveChanges();
                }
                catch (Exception)
                {
                    TempData["message"] =
                        "Unable to delete person";
                }
            }

            return RedirectToAction("Index", new {id});
        }
    }
}
